<template>
  <div>
    <el-form ref="searchForm" :model="searchForm" >
      <el-form-item>
        <el-col :span="8">
          <el-input v-model="searchForm.wd" placeholder="日志动作、操作人、真实姓名" @keyup.enter.native="submitForm('searchForm')"></el-input>
        </el-col>
        <el-col :span="3"><el-button type="primary" style="margin-left:10px" @click="searchForm.currentPage = 1;submitForm('searchForm')">搜索</el-button></el-col>
      </el-form-item>
      <el-form-item label="操作时间">
        <el-col :span="4">
          <el-date-picker type="datetime" placeholder="选择日期时间" v-model="searchForm.startTime" style="width: 100%;"></el-date-picker>
        </el-col>
        <span class="split">-</span>
        <el-col :span="4">
          <el-date-picker type="datetime" placeholder="选择日期时间" v-model="searchForm.endTime" style="width: 100%;"></el-date-picker>
        </el-col>
      </el-form-item>
    </el-form>
    <el-table v-loading="tbLoading" element-loading-text="拼命加载中" :data="logs" stripe style="width: 100%" class="table">
      <el-table-column prop="operationName" label="动作" className="is-center">
      </el-table-column>
      <el-table-column label="操作时间" className="is-center">
        <template slot-scope="scope">
          {{dateFormat(scope.row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column prop="username" label="操作人" className="is-center">
      </el-table-column>
      <el-table-column prop="realname" label="真实姓名" className="is-center">
      </el-table-column>
      <el-table-column prop="result" label="结果" className="is-center">
        <template slot-scope="scope">
          <div v-if="scope.row.result===1">成功</div>
          <div v-else>失败</div>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        :current-page.sync="searchForm.currentPage"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="page.total"
        @current-change="submitForm('searchForm')">
      </el-pagination>
    </div>
  </div>
</template>
<script>
  import {mapState, mapActions} from 'vuex'
  import moment from 'moment'
  export default{
    data () {
      return {
        searchForm: {
          wd: '',
          startTime: '',
          endTime: '',
          currentPage: 1,
          uid: null
        },
        tbLoading: true
      }
    },
    mounted: function () {
      this.getSysLogs().then(() => {
        this.tbLoading = false
      })
    },
    computed: {
      ...mapState({
        logs: state => state.sysManage.logs.list,
        page: state => state.sysManage.logs.page
      })
    },
    methods: {
      ...mapActions(['getSysLogs', 'getAutoComplete']),
      // 日期格式化
      dateFormat: function (date) {
        return this.$date.format(new Date(date), 'YYYY-MM-DD HH:mm:ss')
      },
      submitForm: function (form) {
        this.tbLoading = true
        let startTime = this.searchForm.startTime ? moment(this.searchForm.startTime).format('YYYY-MM-DD HH:mm:ss') : null
        let endTime = this.searchForm.endTime ? moment(this.searchForm.endTime).format('YYYY-MM-DD HH:mm:ss') : null
        let page = this.searchForm.currentPage
        this.getSysLogs({
          page,
          startTime,
          endTime,
          wd: this.searchForm.wd
        }).then(() => {
          this.tbLoading = false
        })
      }
    }
  }
</script>
<style scoped>
  .split{float:left;width:20px;text-align: center;}
  .pagination{float:right;margin-top:30px;}
</style>
